#._*_.coding:utf-8_*_
import  requests,time,random,os
from lxml import etree

headers = {
    'Referer': 'http://www.mzitu.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}

def get_name(imgname):
    a = random.randrange(10000)
    b = random.randrange(10000)
    imgname = str(a)+str(b)+imgname
    return imgname


def write_img(img_link,item):
    global path1
    imgname = img_link[-8:]
    fp = open(path1+'/'+ get_name(imgname), 'wb')
    fp.write(item.content)
    fp.close()

#找到图片链接
def get_img(url,urls):
    header = {
        'Referer': urls,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
    }
    get_img = requests.get(url, headers=header)
    selector = etree.HTML(get_img.text)
    img_link = selector.xpath('/html/body/div[2]/div[1]/div[3]/p/a/img/@src')[0]
    item = requests.get(img_link, headers=headers)
    write_img(img_link,item)

#构造内部图片链接
def get_img_link(href):
    res_img = requests.get(href,headers=headers)
    select_img = etree.HTML(res_img.text)
    all_pages =int( select_img.xpath("//div/a[5]/span/text()")[0])
    img_urls = [href+'/'+'{}'.format(str(i)) for i in range(1,all_pages)]
    for url in img_urls:
        urls = href+'/'+'{}'.format(str(random.randrange(all_pages)))
        get_img(url,urls)



#爬取首页面图片链接
def get_imgs_links(links):
    global path,path1
    for link in links:
        title = link.xpath('li/span/a/text()')[0]
        hrefs = link.xpath('li/span/a/@href')
        print("开始爬取......{}".format(title))
        start = time.time()
        path1 = path + title[:4]
        os.mkdir(path=path+title[:4])
        for herf in  hrefs:
            get_img_link(herf)
        print("用时：{}秒！".format(str(time.time()-start)))


path = "./meizi/"
#获取总页数
res_title = requests.get('http://www.mzitu.com/')
select_title = etree.HTML(res_title.text)
pages =int (select_title.xpath('// nav / div / a/ text()')[-2])
#构造网页地址
urls = ['http://www.mzitu.com/page/{}'.format(str(i)) for i in range(1,pages+1)]
for url in urls:
    res_body = requests.get(url,headers=headers)
    select_body = etree.HTML(res_body.text)  #选择页面
    links = select_body.xpath('//*[@id="pins"]')
    get_imgs_links(links)